package com.kinghood.productcenter.common.model.dto.base;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.kinghood.productcenter.common.model.enums.base.AttributeValueCodeEnum;
import com.kinghood.sharding.model.KingHoodDefaultSort;
import com.kinghood.sharding.model.KingPageReqDTO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 列表查询dto
 *
 * @author jp
 * @date 2023/12/21
 */
@Data
public class ListProductQueryDTO extends KingPageReqDTO implements KingHoodDefaultSort, Serializable {
    public static final String DEFAULT_COLUMN = "createTime";

    /**
     * 款号
     */
    private String styleNumber;
    private List<String> styleNumberList;

    /**
     * 款式名
     */
    private String styleName;
    private List<String> styleNameList;

    /**
     * 商品状态（）
     */
    private Integer status;

    /**
     * 品牌id
     */
    private List<Long> brandList;

    /**
     * 货类id
     */
    private List<Long> goodsList;

    /**
     * 品类id
     */
    private List<Long> categoryList;

    /**
     * 标签id
     */
    private List<Long> labelList;

    /**
     * 编码后缀
     */
    private String codeSuffix;

    /**
     * 开始时间
     */
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    private LocalDateTime endTime;

    /**
     * 开始更新时间
     */
    private LocalDateTime startUpdateTime;

    /**
     * 结束更新时间
     */
    private LocalDateTime endUpdateTime;

    /**
     * 要显示的属性
     */
    private List<String> showAttributeList;

    /**
     * 搜索的属性
     */
    private List<Attribute> searchAttributeList;

    /**
     * 要下载数据的id列表
     */
    private List<Long> idList;

    /**
     * 图片批量导出图片类型，factory：工厂图，hand：手拍图，hd：高清图，operation：运营图
     */
    private String pictureType;

    /**
     * 文件名
     */
    private String fileName;

    /**
     * sheet名称
     */
    private String sheetName;

    /**
     * 要下载字段
     */
    private List<String> downloadField;

    /**
     * 固定的基础属性
     */
    private List<String> baseField;

    /**
     * 要下载基本字段
     */
    private List<String> downloadBaseField;

    /**
     * 要下载的动态属性code
     */
    private List<String> attributeField;

    /**
     * 是否具有产品属性
     */
    private boolean productAttributeFlag = false;
    /**
     * 是否具有产品sku属性
     */
    private boolean productSkuAttributeFlag = false;
    /**
     * 是否具有产品sku-extra属性
     */
    private boolean productSkuExtrasAttributeFlag = false;
    /**
     * 是否具有产品sku spplier属性
     */
    private boolean productSkuSupplierAttributeFlag = false;

    public ListProductQueryDTO() {
//        初始化基础字段
        baseField = new ArrayList<>();
        baseField.add("styleNumber");
        baseField.add("styleName");
        baseField.add("statusTxt");
        baseField.add("brandName");
        baseField.add("goodsName");
        baseField.add("categoryName");
        baseField.add("labelName");
        baseField.add("picture");
        baseField.add("createTime");
        baseField.add("createUserName");
        baseField.add("updateTime");
        baseField.add("updateUserName");
    }

    @Data
    public static class Attribute {
        private Long id;
        private String code;
        private String value;
        private List<String> valueList;
        private String min;
        private String max;
        private String valueCode;
    }

    @Override
    public List<OrderItem> getDefaultSort() {
        OrderItem one = OrderItem.desc(DEFAULT_COLUMN);
        return Collections.singletonList(one);
    }

    @Override
    public List<OrderItem> getTailSort() {
        return null;
    }

    /**
     * 要下载字段是否包含属性字段
     *
     * @author jp
     * @date 2023/12/25
     */
    public boolean downloadFieldHasAttribute() {
        for (String s : downloadField) {
            if (!baseField.contains(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取下载基本字段
     *
     * @author jp
     * @date 2023/12/26
     */
    public List<String> getDownloadBaseField() {
        if (Objects.isNull(downloadBaseField)) {
            downloadBaseField = new ArrayList<>();
            downloadBaseField.addAll(downloadField);
            downloadBaseField.retainAll(baseField);
        }
        return downloadBaseField;
    }

    /**
     * 获取真实属性(去除款式表的基础字段)
     *
     * @author jp
     * @date 2023/12/25
     */
    public List<String> getRealAttribute() {
        if (Objects.isNull(attributeField)) {
            attributeField = new ArrayList<>();
            attributeField.addAll(downloadField);
            attributeField.removeAll(baseField);
        }
        return attributeField;
    }

    /**
     * 样式编号处理，单个编号模糊查询，多个精鹊查询
     *
     * @return
     * @author jp
     * @date 2024/01/11 15:27
     */
    public void processingData() {
        if (StringUtils.isNotBlank(styleNumber) && CollectionUtils.isEmpty(styleNumberList)) {
            this.styleNumberList = Arrays.asList(styleNumber.split(","));
        }
        if (StringUtils.isNotBlank(styleName) && CollectionUtils.isEmpty(styleNameList)) {
            this.styleNameList = Arrays.asList(styleName.split(","));
        }
        if (CollectionUtils.isNotEmpty(styleNumberList)) {
            if (styleNumberList.size() == 1) {
                styleNumber = styleNumberList.get(0);
                styleNumberList = null;
            } else {
                styleNumber = null;
            }
        }
        if (!CollectionUtils.isEmpty(searchAttributeList)) {
            searchAttributeList.removeIf(attribute -> StringUtils.isBlank(attribute.getValue()));
            for (Attribute attribute : searchAttributeList) {
                if (StringUtils.equals(AttributeValueCodeEnum.NUMERICAL.getCode(), attribute.getValueCode())
                        || StringUtils.equals(AttributeValueCodeEnum.DATE.getCode(), attribute.getValueCode())) {
                    String[] arr = attribute.getValue().split(",");
                    attribute.setMin(arr[0]);
                    attribute.setMax(arr[1]);
                } else if (StringUtils.equals(AttributeValueCodeEnum.LEVEL_SELECT.getCode(), attribute.getValueCode())
                        || StringUtils.equals(AttributeValueCodeEnum.CHECK_BOX.getCode(), attribute.getValueCode())
                        || StringUtils.equals(AttributeValueCodeEnum.SELECT.getCode(), attribute.getValueCode())) {
                    attribute.setValueList(Arrays.asList(attribute.getValue().split(",")));
                }
            }
        }
    }
}
